PACKAGE BODY Console_Interface.Menus IS

  PROCEDURE Set_Name (A_Menu : IN OUT Menu ; Name : IN String) IS
  BEGIN
    A_Menu.Name := To_Unbounded_String (Name);
  END Set_Name;


  PROCEDURE Add_Item (A_Menu : IN OUT Menu ; Index : IN String; Label : IN String; A_Proc : ACCESS PROCEDURE) IS
  BEGIN
    A_Menu.Items.Insert (To_Unbounded_String (Index), To_Unbounded_String (Label));
    A_Menu.Actions.Insert (To_Unbounded_String (Index), A_Proc);
  END Add_Item;

  PROCEDURE Execute (A_Menu : IN Menu) IS

    Curs             : Item_Hash.Cursor;
    Cmd_Selection    : Unbounded_String;
    Action_Selection : Action;
  BEGIN

    LOOP
      LOOP
	Curs := Item_Hash.First (A_Menu.Items);
	Ada.Text_IO.New_Line;
	Put_Sub_Title (To_String (A_Menu.Name));

	WHILE Item_Hash.Has_Element (Curs) LOOP
	  Ada.Text_IO.Put_Line (
			 "[" & To_String (Item_Hash.Key (Curs)) & " ] - " &
			   To_String (Item_Hash.Element (Curs)));
	  Item_Hash.Next (Curs);
	END LOOP;
	Ada.Text_Io.New_Line;
	Ada.Text_Io.Put_Line ("Veuillez saisir une commande :");
	Ada.Text_Io.Put_Line ("-------------------------------------");


	Ada.Text_Io.Flush;
	Cmd_Selection := To_Unbounded_String (Ada.Text_IO.Get_Line);

	EXIT WHEN A_Menu.Items.Contains (Cmd_Selection);
	Ada.Text_Io.Put_Line ("Commande inconnue... [entrer]");
	Cmd_Selection := To_Unbounded_String (Ada.Text_IO.Get_Line);
	Clear;
      END LOOP;

      EXIT WHEN Cmd_Selection = A_Menu.Close_Key;
      Clear;
      BEGIN
	Action_Selection := A_Menu.Actions.Element (Cmd_Selection);
	Action_Selection.ALL;

      EXCEPTION
	WHEN Error : OTHERS =>
	  Ada.Text_IO.Put_Line ("Une exception a ete levee : ");
	  Ada.Text_IO.Put_Line (Ada.Exceptions.Exception_Name (Error));
	  Ada.Text_IO.Put_Line (Ada.Exceptions.Exception_Message (Error));
	  Ada.Text_Io.Put_Line ("[entrer]");
	Cmd_Selection := To_Unbounded_String (Ada.Text_IO.Get_Line);
      END;
      Ada.Text_Io.Flush;
      Clear;
    END LOOP;
  END Execute;

  PROCEDURE Add_Exit_Item (A_Menu : IN OUT Menu ; Index : IN String; Label : IN String) IS
  BEGIN
    A_Menu.Items.Insert (To_Unbounded_String (Index), To_Unbounded_String (Label));
    A_Menu.Close_Key := To_Unbounded_String (Index);
  END Add_Exit_Item;

  FUNCTION Equivalent_Key (Left, Right : Unbounded_String) RETURN Boolean IS
  BEGIN
    RETURN Left = Right;
  END Equivalent_Key;

END Console_Interface.Menus;


